package com.tyqx.hashchainbiz.service.impl;

// import com.alibaba.fastjson.JSON;
import com.tyqx.hashchainbean.module.DonateProduct;
import com.tyqx.hashchainbean.module.HashIndex;
import com.tyqx.hashchainbiz.service.DonateProductService;
import com.tyqx.hashchaindao.mapper.DonateProductMapper;
import com.tyqx.hashchaindao.mapper.HashIndexMapper;
import com.tyqx.hashchainutil.common.HashValueUtil;
import com.tyqx.hashchainutil.common.mongodb.ImagePathHashValueUtil;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service("donateProductService")
public class DonateProductServiceImpl implements DonateProductService {

    @Resource
    private HashIndexMapper hashIndexMapper;

    @Resource
    private DonateProductMapper donateProductMapper;

    @Resource
    private MongoTemplate mongoTemplate;

    @Async
    public Integer donateProduct(DonateProduct donateProduct) throws Exception {
        String[] donateIds=donateProduct.getDonateId().split(";");
        Integer count=0;
        if(donateIds.length>1){
            for (int i=0;i< donateIds.length;i++){
                Date currentDate = new Date();
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Date createTime=sdf.parse(sdf.format(currentDate));
                DonateProduct donateProduct1=new DonateProduct();
                donateProduct1.setCreateTime(createTime);
                donateProduct1.setUpdateTime(createTime);
                donateProduct1.setAttachment(donateProduct.getAttachment());
                donateProduct1.setDonatee(donateProduct.getDonatee());
                donateProduct1.setDonateName(donateProduct.getDonateName());
                donateProduct1.setDonateTime(donateProduct.getDonateTime());
                donateProduct1.setDonateType(donateProduct.getDonateType());
                donateProduct1.setDonateUnit(donateProduct.getDonateUnit());
                donateProduct1.setEffectTime(donateProduct.getEffectTime());
                donateProduct1.setFileNo(donateProduct.getFileNo());
                donateProduct1.setPackageNum(donateProduct.getPackageNum());
                donateProduct1.setPoint(donateProduct.getPoint());
                donateProduct1.setProduceTime(donateProduct.getProduceTime());
                donateProduct1.setProductType(donateProduct.getProductType());
                donateProduct1.setShipId(donateProduct.getShipId());
                donateProduct1.setShipper(donateProduct.getShipper());
                donateProduct1.setTemp(donateProduct.getTemp());
                donateProduct1.setUserName(donateProduct.getUserName());
                donateProduct1.setDonateId(donateIds[i]);
                String donateProductStr= donateProduct1.toString();
                HashIndex hashIndex= HashValueUtil.getHashValue(donateProductStr);
                donateProduct1.setHashValue(hashIndex.getHashValue());

                String attachmentStr=donateProduct.getAttachment();
                if (!"".equals(attachmentStr) && attachmentStr != null) {
                    String collectionName = "donateProduct";
                    String imageHashValue = ImagePathHashValueUtil.insertAttachment(attachmentStr, mongoTemplate, collectionName);
                    donateProduct.setAttachment(imageHashValue);
                }

                Integer flag=hashIndexMapper.insert(hashIndex);
                count=donateProductMapper.addProduct(donateProduct1);
            }
        }else{
            Date currentDate = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date createTime=sdf.parse(sdf.format(currentDate));
            donateProduct.setDonateId(donateIds[0]);
            donateProduct.setCreateTime(createTime);
            donateProduct.setUpdateTime(createTime);
            String donateProductStr= donateProduct.toString();


            String attachmentStr=donateProduct.getAttachment();
            if (!"".equals(attachmentStr) && attachmentStr != null) {
                String collectionName = "donateProduct";
                String imageHashValue = ImagePathHashValueUtil.insertAttachment(attachmentStr, mongoTemplate, collectionName);
                donateProduct.setAttachment(imageHashValue);
            }
            HashIndex hashIndex=HashValueUtil.getHashValue(donateProductStr);
            donateProduct.setHashValue(hashIndex.getHashValue());
            Integer flag=hashIndexMapper.insert(hashIndex);
            count=donateProductMapper.addProduct(donateProduct);
        }
        return count;
    }

    @Override
    public List<DonateProduct> getDonateProductById(String donateId, String userName) {
        return donateProductMapper.getDonateProductById(donateId);
    }

    @Override
    public List<DonateProduct> getDonateProductByTime(Date startTime, Date endTime, String userName) {
        return donateProductMapper.getDonateProductByTime(startTime, endTime, userName);
    }
}
